package cuongnh.project.japanesediary.vocabulary.ui.view;

import java.util.Collections;
import java.util.List;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cuongnh.coreframework.ui.workbench.common.view.AutoUpdateView;
import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.common.CommonUtils;
import cuongnh.project.japanesediary.vocabulary.model.Kanji;
import cuongnh.project.japanesediary.vocabulary.ui.common.composite.SearchComposite;
import cuongnh.project.japanesediary.vocabulary.ui.editor.kanji.KanjiEditor;
import cuongnh.project.japanesediary.vocabulary.ui.editor.kanji.KanjiEditorInput;
import de.ralfebert.rcputils.tables.TableViewerBuilder;

@Component("kanjiListView")
@Scope("prototype")
public class KanjiListView extends AutoUpdateView {

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.ui.view.KanjiListView"; //$NON-NLS-1$
	private TableViewerBuilder tblKanjiList;

	private SearchComposite search;
	@Autowired
	private CommonServices commonServices;
	private List<Kanji> kanjis;
	private Kanji selectedKanji;

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());

	public KanjiListView() {
	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		// Search control
		search = new SearchComposite(parent);

		// Separate composite to embed the table
		Composite tableComposite = new Composite(parent, SWT.NONE);

		tblKanjiList = new TableViewerBuilder(tableComposite, SWT.BORDER
				| SWT.FULL_SELECTION);
		tblKanjiList.createColumn("Kanji").bindToProperty("kanji")
				.setPercentWidth(30).build();
		tblKanjiList.createColumn("Vietnamese meaning").bindToProperty("vnMeaning")
				.setPercentWidth(70).build();

		// Filter
		tblKanjiList.getTableViewer().addFilter(new ViewerFilter() {
			@Override
			public boolean select(Viewer viewer, Object parentElement,
					Object element) {
				Kanji kanji = (Kanji) element;
				String text = String.valueOf(search.observeText().getValue());
				if (kanji.getKanji().toLowerCase()
						.contains(text.toLowerCase())) {
					return true;
				}

				return false;
			}

		});

		tblKanjiList.getTableViewer().addSelectionChangedListener(
				new ISelectionChangedListener() {

					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						if (event.getSelection().isEmpty()) {
							return;
						}

						if (event.getSelection() instanceof IStructuredSelection) {
							IStructuredSelection selection = (IStructuredSelection) event
									.getSelection();
							selectedKanji = (Kanji) selection
									.getFirstElement();
						}
					}
				});

		search.observeText().addChangeListener(
				new org.eclipse.core.databinding.observable.IChangeListener() {
					@Override
					public void handleChange(
							org.eclipse.core.databinding.observable.ChangeEvent event) {
						tblKanjiList.getTableViewer().refresh();
					}
				});

		// Layout
		GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parent);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
				.applyTo(search);
		GridDataFactory.fillDefaults().grab(true, true).applyTo(tableComposite);
		
		initData();

		createActions();
		initializeToolBar();
		initializeMenu();
		CommonUtils.kanjisHookDoubleClickCommand(tblKanjiList.getTableViewer());
		this.setInterval(1);
		this.startAutoReload();
	}
	
	public void initData() {
		kanjis = (commonServices.getKanjiDao() != null) ? commonServices
				.getKanjiDao().getAll() : Collections
				.<Kanji> emptyList();
		tblKanjiList.setInput(kanjis);
	}

	public void dispose() {
		toolkit.dispose();
		super.dispose();
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager manager = getViewSite().getActionBars().getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	@Override
	public void update() {
		initData();
		
	}

}
